<?php
use GatewayClient\Gateway;
global $_GPC, $_W;
$rid = intval($_GPC['rid']);

$uniacid = $_W['uniacid'];

$countId  = $_GPC['countId'];

$type = $_GPC['type'];

$reply = pdo_fetch("SELECT * FROM ".tablename('haoman_dpm_countmoneyreply')." WHERE rid=:rid",array(':rid'=>$rid));

$pici =empty($reply['pici'])?1:$reply['pici'];

$c =100001;

$group = $rid.$pici.$uniacid*$c;

//Gateway::sendToAll(json_encode($group));

        if(empty($reply)){
            $data = array(
                'ResultType' => 2,
                'Message' => "活动不存在"
            );
            echo json_encode($data);
            exit();
        }elseif ($reply['iscount']!=1){
            $data = array(
                'ResultType' => 2,
                'Message' => "数钱活动未开启"
            );
            echo json_encode($data);
            exit();
        }

        if($type == 'start'){

            pdo_update('haoman_dpm_countmoneyreply', array('status' => 1,'createtime' =>time()), array('id' => $reply['id']));

            $count_money_id = pdo_fetchcolumn( " SELECT id FROM ".tablename('haoman_dpm_countmoneyuser')." WHERE rid='".$rid."' and pici =  '".$countId."'" );

            if($count_money_id<1){
                $data = array(
                    'ResultType' => 2,
                    'Message' => "还没有参与用户"
                );
                echo json_encode($data);
                exit();
            }else{
                $data = array(
                    'ResultType' => 1
                );
                echo json_encode($data);

//                $message = [
//                    'type' => 'logMessage',
//                    'data' => 0
//                ];
//                Gateway::sendToGroup($group, json_encode($message));

//            Gateway::sendToAll(json_encode($group));
                exit();
            }


        }elseif ($type==2){

            pdo_update('haoman_dpm_countmoneyreply', array('status' => 2), array('id' => $reply['id']));

            $Counts = pdo_fetchall("SELECT * FROM " . tablename('haoman_dpm_countmoneyuser') . " WHERE pici = :pici and  rid = " . $rid . " and uniacid=" . $uniacid . " order by `count` desc LIMIT 5",array(':pici'=>$countId));

//            $message = [
//                'type' => 'gameover',
//                'data' => 0
//            ];
//            Gateway::sendToGroup($group, json_encode($message));

            $uid2 = $rid.$pici.$uniacid*$c;
//            $message2 = [
//                'type' => 'gameover2',
//                'data' => $Counts
//            ];
//            Gateway::sendToUid($uid2, json_encode($message2));

            $data = array(
                'flag' => 1,
                'msg' => "活动状态修改正确",
            );

            echo json_encode($data);
            exit();
        }else{
            pdo_update('haoman_dpm_countmoneyreply', array('status' => 0,'pici'=>$reply['pici']+1), array('id' => $reply['id']));

            $data = array(
                'flag' => 1,
                'msg' => "活动状态修改正确",
            );

            echo json_encode($data);
            exit();
        }



